package dhm.com.dhmshop.updata.activity;

import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.ldf.calendar.Const;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import dhm.com.dhmshop.R;
import dhm.com.dhmshop.adapter.mine.FunctionAdapter;
import dhm.com.dhmshop.base.BaseActiity;
import dhm.com.dhmshop.base.Presenter.PressenterImpl;
import dhm.com.dhmshop.base.netWork.Constant;
import dhm.com.dhmshop.base.netWork.LoginContract;
import dhm.com.dhmshop.entity.Bean;
import dhm.com.dhmshop.entity.FunctionBean;
import dhm.com.dhmshop.utils.SpUtils;
import dhm.com.dhmshop.view.mine.myshop.ChangeorderActivity;

public class FunctionActivity extends BaseActiity implements LoginContract.IView {

    @BindView(R.id.text_type)
    TextView textType;
    @BindView(R.id.edit_count)
    EditText editCount;
    @BindView(R.id.information)
    EditText information;
    private PressenterImpl pressenter;
    private FunctionAdapter functionAdapter;
    private FunctionBean functionBean;
    private String typeid = "";
    private PopupWindow popWindow;

    @Override
    protected int getLayout() {
        return R.layout.activity_function;
    }

    @Override
    protected void initView() {
        ButterKnife.bind(this);
    }

    @Override
    protected void initData() {
        pressenter = new PressenterImpl();
        pressenter.attachView(this);
        showPopwindow();
    }


    @OnClick({R.id.back, R.id.question, R.id.edit_login})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.question:
                setBackgroundAlpha(0.5f);
                popWindow.showAtLocation(view, Gravity.BOTTOM, 0, 0);
                break;
            case R.id.edit_login:
                if (typeid.equals("")) {
                    Toast.makeText(FunctionActivity.this, "请选择反馈类型", Toast.LENGTH_SHORT).show();
                    return;
                }
                String feedbackContent = editCount.getText().toString();
                String contactInfo = information.getText().toString();
                if (feedbackContent==null||feedbackContent.equals("")){
                    Toast.makeText(this, "请描述反馈问题", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (contactInfo==null||contactInfo.equals("")){
                    Toast.makeText(this, "请填写联系方式", Toast.LENGTH_SHORT).show();
                    return;
                }
                String uid = SpUtils.getString(this, "uid");
                Map<String,String> map=new HashMap<>();
                map.put("token", Constant.TOKEN);
                map.put("uid",uid);
                map.put("feedbackType",typeid);
                map.put("feedbackContent",feedbackContent);
                map.put("contactInfo",contactInfo);
                pressenter.sendMessage(this,Constant.FeedBackAdd,map, Bean.class);

                break;
            default:
        }
    }

    @Override
    public void requesta(Object data) {
        if (data instanceof FunctionBean) {
            functionBean = (FunctionBean) data;
            functionAdapter.setShopList(functionBean.getData());
        }else if (data instanceof Bean){
            Bean bean= (Bean) data;
            if (bean.getCode()==1){
                Toast.makeText(this, bean.getMessage(), Toast.LENGTH_SHORT).show();
                finish();
            }
        }
    }

    @Override
    public void fail(String error) {

    }

    private void showPopwindow() {
        View popView = View.inflate(this, R.layout.popup_questiontype, null);
        Button sure = popView.findViewById(R.id.sure);


        RecyclerView recy = popView.findViewById(R.id.recy);
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        functionAdapter = new FunctionAdapter(this);
        functionAdapter.setaddClick(new FunctionAdapter.OnShopClick() {
            @Override
            public void item(List<FunctionBean.DataBean> beanList) {
                functionBean.setData(beanList);
                functionAdapter.setShopList(functionBean.getData());
            }
        });

        recy.setAdapter(functionAdapter);
        recy.setLayoutManager(manager);
        sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                for (int i = 0; i < functionBean.getData().size(); i++) {
                    if (functionBean.getData().get(i).isChecked()) {
                        textType.setText(functionBean.getData().get(i).getDataName());
                        typeid = functionBean.getData().get(i).getDataVal() + "";
                    }
                }

                if (typeid.equals("")) {
                    Toast.makeText(FunctionActivity.this, "请选择反馈类型", Toast.LENGTH_SHORT).show();
                    return;
                }

                popWindow.dismiss();
            }
        });


        popWindow = new PopupWindow(popView, ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT, true);
        popWindow.setAnimationStyle(R.style.AnimBottom);
        popWindow.setFocusable(true);
        popWindow.setTouchable(true);
        popWindow.setBackgroundDrawable(new BitmapDrawable());
        popWindow.setOutsideTouchable(true);// 设置允许在外点击消失


        popWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                // popupWindow隐藏时恢复屏幕正常透明度
                setBackgroundAlpha(1.0f);
            }
        });

        Map<String, String> map = new HashMap<>();
        map.put("token", Constant.TOKEN);
        pressenter.sendMessage(this, Constant.FeedBack, map, FunctionBean.class);
    }




    /**
     * 设置添加屏幕的背景透明度
     *
     * @param bgAlpha 屏幕透明度0.0-1.0 1表示完全不透明
     */
    public void setBackgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = getWindow()
                .getAttributes();
        lp.alpha = bgAlpha;
        getWindow().setAttributes(lp);
    }


}
